---- 闪电效果管理类
local streak = nil --闪电
local runBegin = false --开始
local lastPos = nil --
local curDetail = 30
local curDisplace = 100
local runPoss = {}


-- 生成闪电路线
local function _drawLineShandian(x1, y1, x2, y2, displace, newPoslins)

    if displace < curDetail then
        table.insert(newPoslins, ccp(x1,y1))
    else
        local mid_x = (x2+x1)/2
        local mid_y = (y2+y1)/2
        mid_x = mid_x + (math.random()-0.5)*displace
        mid_y = mid_y + (math.random()-0.5)*displace
        --print("mid:",mid_x,mid_y)
        _drawLineShandian(x1,y1,mid_x,mid_y,displace/2,newPoslins)
        _drawLineShandian(x2,y2,mid_x,mid_y,displace/2,newPoslins)
    end
end


--------------------- 游戏道具按钮类
ClassLightning = {}
--这句是重定义元表的索引，必须要有，
ClassLightning.__index = ClassLightning
--模拟构造体，一般名称为new()
function ClassLightning:new(llist, func)
    local self = {}   
    setmetatable(self, ClassLightning)   --必须要有
    
    self.runPoss = {}
    self.streak = nil
    self.onEnds = false
    self.linghtlist = llist
    self.index = 1
    self.func = func
    
    self.streak = CCMotionStreak:create(2.5, 24, 24, ccc3(255, 255, 255), GetPathRes("test2.png"))
    CCDirector:sharedDirector():getRunningScene():addChild(self.streak)
    
    return self  
end

function ClassLightning:update(dt)
    if self.onEnds then
        return
    end
    
    if #self.runPoss > 0 then
        local pos = self.runPoss[1]
        self.streak:setPosition(pos)
        --print(pos.x, pos.y)
        table.remove(self.runPoss, 1)
        if #self.runPoss == 0 then
        end
    else
        --如果还有线,则生成飞行
        if #self.linghtlist > self.index then
            self.index = 1
            
            local lobj = self.linghtlist[self.index]
            self.index = self.index + 1
            --_func_deleteGem(lobj.obj,nil,0.5)
            --print("///////////___",lobj.objlist)
            if lobj.objlist ~= nil then
                table.foreach(lobj.objlist, function(k1,obj)
                    if obj ~= nil then
                        if k1 == 1 then
                            local mx,my = getGemPos(obj.row, obj.col)
                            local anim = GFunc_CreateAnimation("randombomb_0000", 1, 11, 1, 0.1, function(_animobj)
                                CCDirector:sharedDirector():getRunningScene():removeChild(_animobj, true)
                            end)
                            anim:setPosition(mx, my)
                            CCDirector:sharedDirector():getRunningScene():addChild(anim)
                        end
                        _func_deleteGem(obj,nil,0.5, Item_BomRandom)
                    end
                end)
            end
            
            --起点
            local newPos1 = lobj.pos
            local bpx = newPos1.x
            local bpy = newPos1.y
            --终点,下一个目标点
            local lobj = self.linghtlist[self.index]
            local newPos2 = lobj.pos
            local tpx = newPos2.x
            local tpy = newPos2.y
            --记录点
            --self.lastPos = ccp(tpx,tpy)
            
            local newPoslist = {}
            _drawLineShandian(bpx,bpy,tpx,tpy, curDisplace, newPoslist)
            
            --- 排序
            function sortPosss(a, b)
                local vlenght1 = ccpLength(ccp(bpx - a.x, bpy - a.y))
                local vlenght2 = ccpLength(ccp(bpx - b.x, bpy - b.y))
                return vlenght2 > vlenght1
            end
            table.foreach(newPoslist, function(k,p)
                table.insert(self.runPoss, p)
            end)
            table.sort(self.runPoss, sortPosss)
            
            self:update(dt)
            table.remove(self.linghtlist, 1)
            
            Play_Effect(sme_BomH)
        else
            Play_Effect(sme_BomH)
            table.foreach(self.linghtlist, function(k,lobj)
                table.foreach(lobj.objlist, function(k1,obj)
                    if obj ~= nil then
                        if k1 == 1 then
                            local mx,my = getGemPos(obj.row, obj.col)
                            local anim = GFunc_CreateAnimation("randombomb_0000", 1, 11, 1, 0.1, function(_animobj)
                                CCDirector:sharedDirector():getRunningScene():removeChild(_animobj, true)
                            end)
                            anim:setPosition(mx, my)
                            CCDirector:sharedDirector():getRunningScene():addChild(anim)
                        end
                        _func_deleteGem(obj,nil,0.5, Item_BomRandom)
                    end
                end)
            end)
            --local lobj = self.linghtlist[1]
            --table.remove(self.linghtlist, 1)
            --_func_deleteGem(lobj.obj)
            ---全部结束
            self.onEnds = true
            
            if self.func ~= nil then
                self.func()
            end
        end
    end
end



local tableLightning = {}
-- 创建一条新的闪电路线,ltable:路线表 func:到达后的回调
Lightning_New = function(ltable, func)
    local nl = ClassLightning:new(ltable, func)
    table.insert(tableLightning, nl)
    _randomBomsSums = _randomBomsSums + 1
end


local function update(dt)
    local i = 1
    while i <= #tableLightning do
        tableLightning[i]:update(dt)
        if tableLightning[i].onEnds then
            _randomBomsSums = _randomBomsSums - 1
            table.remove(tableLightning, i)
            i = i - 1
        end
        i = i + 1
    end
    --table.foreach(tableLightning, function(k, clight)
    --    clight:update(dt)
    --end)
end
CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(update, 1.0/60, false)


















